package com.opengroupe.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opengroupe.dto.CandidatDto;
import com.opengroupe.dto.SimpleCandidatReponsesQuestionDto;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.CandidatReponseQuestionId;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.common.CommonService;
import com.opengroupe.service.common.CrudService;
import com.opengroupe.service.tablet.TabletService;
import com.opengroupe.util.HeavyResultatQuestionnaire;
import com.opengroupe.utils.QrhWsUtils;

@Service("crqResource")
@Path("/questionnaire")
public class CRQResource {

	@Autowired
	private QrhWsUtils utils;
	
	@Autowired
	private TabletService tabletService;
	
	@Autowired
	private CrudService crudService;
	
	@Autowired
	private CommonService commonService;

	@GET
	@Produces("application/json")
	@Path("/consulterResultat")
	@Transactional
	public String consulterResultat(@QueryParam("emailCandidat") String emailCandidat) {

		CandidatDto candidatDto = new CandidatDto();
		candidatDto.setEmailCandidat(emailCandidat);
				
		Candidat candidat = new Candidat();
		candidat = QrhWsUtils.convertCandidatDtoToCandidat(candidatDto);
		
		HeavyResultatQuestionnaire heavyResultatQuestionnaire = commonService.consulterResultat(candidat);
		if(heavyResultatQuestionnaire != null){
			return heavyResultatQuestionnaire.getLightResultatQuestionnaire();
		}
		return "";
	}
	
	@POST
	@Produces("application/json")
	@Path("/deleteReponseCandidat")
	public String deleteCandidatReponseQuestion(String json) {
		
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
		SimpleCandidatReponsesQuestionDto crqDto = gson.fromJson(json,SimpleCandidatReponsesQuestionDto.class);

		Candidat candidat = (Candidat) crudService.read(crqDto.getEmailCandidat(), Candidat.class.getCanonicalName());
		
		Question question = (Question) crudService.read(crqDto.getIdQuestion(),Question.class.getCanonicalName());

		List<CandidatReponseQuestion> crqs = tabletService.deleteCRQs(candidat,question);
		
		return Boolean.toString(crqs != null && crqs.size() > 0);
	}
	
	
	@POST
	@Produces("application/json")
	@Path("/repondreQuestion")
	public synchronized String repondreQuestion(String json) throws DataAccessException, Exception {
		
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
		SimpleCandidatReponsesQuestionDto crqDto = gson.fromJson(json, SimpleCandidatReponsesQuestionDto.class);
		
		List<String> idReponses = crqDto.getIdReponses();
		
		if(idReponses == null || idReponses.size() <= 0) {
			return Boolean.toString(false);
		}
		
		String emailCandidat = crqDto.getEmailCandidat();
		int idQuestion = crqDto.getIdQuestion();

		Candidat candidat = (Candidat) crudService.read(emailCandidat, Candidat.class.getCanonicalName());
		Question question = (Question) crudService.read(idQuestion, Question.class.getCanonicalName());

		List<Reponse> reponses = new ArrayList<Reponse>();
		for(String strId: idReponses) {
			int idReponse = Integer.parseInt(strId);
			Reponse reponse = (Reponse) crudService.read(idReponse, Reponse.class.getCanonicalName());
			reponses.add(reponse);
		}
		
		if(reponses.size() > 0) {
			List<CandidatReponseQuestionId> resultat = tabletService.repondreQuestion(candidat, question, reponses);
			if(resultat.size() == reponses.size()) {
				return Boolean.toString(true);
			} else {
				return Boolean.toString(false);
			}
		}
		return Boolean.toString(false);
	}
	
	@GET
	@Produces("application/json")
	@Path("/affecterQuestionnaire")
	public synchronized String affecterQuestionnaire(@QueryParam("emailCandidat") String emailCandidat, @QueryParam("categorie") String categorie){
		return utils.buildQuestionnaire(emailCandidat, categorie);
	}
}
